package com.rh.controller;

import com.rh.Enums;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.Subject;
import com.rh.excrptions.DoException;
import com.rh.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@CrossOrigin
@Controller
@ResponseBody
public class SubjectController {
    @Autowired
    private SubjectService subjectService;
    /**
     * 分页数据
     *
     */
    @RequestMapping(value = "/getSubjectPage",method = RequestMethod.GET)
    public Result getSubjectPage(Integer pageNo,Integer pageSize) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 3;
            }
            Page subjectPage = subjectService.getSubjectPage(pageNo, pageSize);
            Result result = new Result();
            result.setData(subjectPage);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
    /**
     * 添加学科
     */
    @RequestMapping(value = "/addSubject",method = RequestMethod.POST)
    public Result addSubject(Subject subject){
        try{
            //int i=1/0;
            subjectService.addMethod(subject);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.ADD_ERROR);
        }
    }
    /**
     *  删除学科
     */
    @RequestMapping(value = "/deleteSubject",method = RequestMethod.GET)
    public Result deleteSubject(Integer subjectId){
        try{
            //int i=1/0;
            subjectService.deleteMethod(subjectId);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.DELETE_ERROR);
        }
    }
    /**
     * 修改学科信息
     */
    @RequestMapping(value = "/updateSubject",method = RequestMethod.POST)
    public Result updateSubject(Subject subject){
        try{
            //int i=1/0;
            subjectService.updateMethod(subject);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.UPDATE_ERROR);
        }
    }
    /**
     * 根据id查询学科信息
     */
    @RequestMapping(value = "/getSubjectById",method = RequestMethod.GET)
    public Result getSubjectById(Integer subjectId) {
        try {
            Subject methodAllById = subjectService.getMethodAllById(subjectId);
            Result result = new Result();
            result.setData(methodAllById);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
}
